<?PHP
/*  stu_fitness_ques  v0.1
 *  Plugin FOR Discuz! X 
 *  By STU Michael && NULL
 */
 
 
if(!defined('IN_DISCUZ')) {
  exit('Access Denied');
}




// $tid =  intval($_G['gp_typeid']);
// $wherestr = $tid ? " AND qclass = '$tid'" : '';
// $myqtid = intval($_G['gp_myqt']);
// $wherestr .= $myqtid == 2  ?  " and  author_id = ".$_G['uid'] : '';
// !empty($_G['gp_srchtxt'])? $wherestr .= " AND  title like '%".htmlspecialchars(trim(substr($_G['gp_srchtxt'],0,20)))."%' " :'' ;
//   if ($myqtid == 3 ) {
//     $magiccount =  DB::result(DB::query("SELECT COUNT(*) FROM ".DB::table('fitness_test_submit')." WHERE user_id = '$_G[uid]'".$wherestr), 0);
//       $sql = "SELECT * FROM ".DB::table('fitness_test_submit')." WHERE user_id = '$_G[uid]' ".$wherestr." ORDER by submit_time DESC LIMIT $start_limit,$perpage"; 
//     }elseif($myqtid == 4 ){
//          $mytopicids = '0'; 
//          $glue = ',';   
//          $query =  DB::query("SELECT questioannaire_id  FROM ".DB::table('fitness_test_submit')." WHERE user_id = '$_G[uid]'");
//          while($value = DB::fetch($query)) {
//           $mytopicids .= $glue.$value['questionnaire_id'];
//          }
//        $magiccount =  DB::result(DB::query("SELECT COUNT(*) FROM ".DB::table('fitness_test_questionnaire')." WHERE id NOT IN('$mytopicids') ".$wherestr), 0);
//          $sql = "SELECT * FROM ".DB::table('fitness_test_quesionnaire')."  WHERE id NOT IN($mytopicids) ".$wherestr ." ORDER by post_time DESC LIMIT $start_limit,$perpage";
//     }else{
//        $magiccount =  DB::result(DB::query("SELECT COUNT(*) FROM ".DB::table('fitness_test_questionnaire')." WHERE 1=1 ".$wherestr." "), 0);
//       $sql = "SELECT * FROM ".DB::table('fitness_test_questionnaire').' WHERE  1=1 '.$wherestr." ORDER by post_time DESC LIMIT $start_limit,$perpage";
//     }
//       $multipage = multi($magiccount, $perpage, $page, "plugin.php?id=nds_up_ques:nds_up_ques".($tid ? '&typeid='.$tid:'').($myqtid ? '&myqt='.$myqtid:'') ); 
//      $query= DB::query($sql);
//         $qclasshtml = '';
//         while($queslist = DB::fetch($query)) {
// //         if($queslist[''] ){
// //          $topictype = '&nbsp;&nbsp;<img src="'.$imgurl.'access_disallow.gif" alt="'.lang('plugin/nds_up_ques', 'typestop').'">';  
// //         }elseif($queslist['exp'] < $_G['timestamp'] ){
// //          $topictype = '&nbsp;&nbsp;<img src="'.$imgurl.'clock.gif" alt="'.lang('plugin/nds_up_ques', 'typeexp').'">'; 
// //         }elseif($ndsopcheck && !$queslist['opcheck'] ){
// //          $topictype = '&nbsp;&nbsp;<img src="'.$imgurl.'access_normal.gif" alt="'.lang('plugin/nds_up_ques', 'typeopcheck').'">'; 
// //         }else{
// //           $topictype = '&nbsp;&nbsp;<img src="'.$imgurl.'access_allow.gif"alt="'.lang('plugin/nds_up_ques', 'typerun').'">';
// //         }
// //              $queslists[$queslist['topicid']]['topictype'] = $topictype;
// //                if ($ndsqclass && $queslist['qclass'] ){
// //               $queslists[$queslist['topicid']]['qclass'] = "<span class='emqclass'>[</span><em><a href='plugin.php?id=nds_up_ques:nds_up_ques&action=index&typeid=$queslist[qclass]'>".$ndsqclass[$queslist['qclass']]."</a></em><span class='emqclass'>]</span>";
// //              }else{
// //               $queslists[$queslist['topicid']]['qclass'] = '';  
// //              }
//               $queslists[$queslist['topicid']]['subject'] = $queslist['subject'];
//           $queslists[$queslist['topicid']]['dateline'] = dgmdate($queslist['dateline']); 
//           $queslists[$queslist['topicid']]['authorid'] = $queslist['authorid'];
//           $queslists[$queslist['topicid']]['author'] = $queslist['author'];
//           $queslists[$queslist['topicid']]['person'] =  $queslist['person'];
//           $queslists[$queslist['topicid']]['post_count'] =  $queslist['post_count'];
//           $queslists[$queslist['topicid']]['exps'] = dgmdate($queslist['exp']); 
//           $queslists[$queslist['topicid']]['ques_mode'] = $queslist['ques_mode'];
//             }
//         $navtitle = lang('plugin/stu_fitness', 'action_121356').' - '.$navtitle;

/**
 * 获取所有问卷列表
 * @return unknown
 */
function getAll($subwhere){
	$where = " where status = 1 ".$subwhere;
  	$questionnaireList = DB::fetch_all("select * from ".DB::table('fitness_questionnaire').$where." order by post_time desc");
  	foreach ($questionnaireList as $key => $value) {
    	$questionnaireList[$key]['author'] = DB::result(DB::query("select username from ".DB::table('common_member')." where uid = ".$value['author_id']));
    	$questionnaireList[$key]['type'] = DB::result(DB::query("select name from ".DB::table('fitness_questionnaire_type')." where id = ".$value['type_id']));
  	}
  	return $questionnaireList;
}

/**
 * 获取我发布的问卷列表
 * @return unknown
 */
function getMine($subwhere){
	$where = " where status = 1 and author_id = ".$_GET['uid'].$subwhere;
  	$questionnaireList = DB::fetch_all("select * from ".DB::table('fitness_questionnaire').$where." order by post_time desc");
  	foreach ($questionnaireList as $key => $value) {
    	$questionnaireList[$key]['author'] = DB::result(DB::query("select username from ".DB::table('common_member')." where uid = ".$value['author_id']));
    	$questionnaireList[$key]['type'] = DB::result(DB::query("select name from ".DB::table('fitness_questionnaire_type')." where id = ".$value['type_id']));
  	}
  	return $questionnaireList;
}

/**
 * 获取我已参加的问卷列表
 * @return unknown
 */
function getDone($subwhere){
	$where = " where user_id = ".$_G[uid].$subwhere;
  	$submitList = DB::fetch_all("select * from ".DB::table('fitness_submit').$where);
  	foreach ($submitList as $key => $value) {
    	$questionnaireList[$key] = DB::fetch_first("select * from ".DB::table('fitness_questionnaire')." where id = ".$value['questionnaire_id']);;
  	}
  	foreach ($questionnaireList as $key => $value) {
    	$questionnaireList[$key]['author'] = DB::result(DB::query("select username from ".DB::table('common_member')." where uid = ".$value['author_id']));
    	$questionnaireList[$key]['type'] = DB::result(DB::query("select name from ".DB::table('fitness_questionnaire_type')." where id = ".$value['type_id']));
  	}
  	return $questionnaireList;
}

/**
 * 获取我未参加的问卷列表
 * @return unknown
 */
function getUndone($subwhere){
  	$doneList = getDone($subwhere);
  	$allList = getAll($subwhere);
  	$questionnaireList = array_diff($doneList,$allList);
  	return $questionnaireList;
}

/**
 * 控制区
 */
if($_GET['typeid']){
	$subwhere = " and type_id = ".$_GET['typeid'];
}else{
	$subwhere = '';
}

$myqtid = $_GET['myqt'];
$questionnaireList = array();

if ($myqtid == 2) {
  	$questionnaireList = getMine($subwhere);
}elseif ($myqtid == 3) {
  	$questionnaireList = getDone($subwhere);
}elseif ($myqtid == 4) {
  	$questionnaireList = getUndone($subwhere);
}else {
  	$questionnaireList = getAll($subwhere);
}

foreach ($questionnaireList as $key => $value) {
	$questionnaireList[$key]['post_time'] = date("Y-m-d H:i:s",$value['post_time']);
	$questionnaireList[$key]['deadline'] = date("Y-m-d H:i:s",$value['deadline']);
}

$megiccount = count($questionnaireList);
$hello = $magiccount;
$multipage = multi($magiccount, $perpage, $page, "plugin.php?id=nds_up_ques:nds_up_ques".($tid ? '&typeid='.$tid:'').($myqtid ? '&myqt='.$myqtid:''));
include template('stu_fitness:fitness_index');

?>